import Vue from 'vue';

/* eslint-disable */
Vue.prototype.$common = {
  /**
   * 剪切字符串
   * @ str 原字符串
   * @ 最大长度
   */
  MsgCutWord(str, num) {
    if (str == null) {
      /* eslint-disable no-param-reassign */
      str = '';
    }
    let result = str;
    const wordLength = str.length;
    if (wordLength > num) {
      result = str.substr(0, num);
      result += '...';
    }
    return result;
  },

  /**
   *  修改日期格式年月日
   *  @ jsondate json日期
   *  @ splitchar 分隔符
   */
  ChangeDateFormat(jsondate, splitchar) {
    if (!jsondate) {
      return '';
    }
    const reg = /^\/Date\((\d+)\)\/$/g;
    let date = null;
    if (reg.test(jsondate)) {
      date = new Date(Number.parseInt(jsondate.replace(reg, '$1'), 10));
    } else if (/^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}.\d+Z$/gi.test(jsondate)) {
      jsondate = jsondate.replace(/(^.+)(T)(.+)(\..+Z)/gi, '$1 $3');
      date = new Date(jsondate);
    } else {
      date = new Date(jsondate);
    }
    const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
    const currentDate = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
    return date.getFullYear() + splitchar + month + splitchar + currentDate;
  },

  /**
   *  修改日期格式年月日
   *  @ jsondate json日期
   *  @ splitchar 分隔符
   */
  ChangeDateFormat_hhmmss(jsondate, splitchar) {
    if (!jsondate) {
      return '';
    }
    const reg = /^\/Date\((\d+)\)\/$/g;
    let date = null;
    if (reg.test(jsondate)) {
      date = new Date(Number.parseInt(jsondate.replace(reg, '$1'), 10));
    } else if (/^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}.\d+Z$/gi.test(jsondate)) {
      jsondate = jsondate.replace(/(^.+)(T)(.+)(\..+Z)/gi, '$1 $3');
      date = new Date(jsondate);
    } else {
      date = new Date(jsondate);
    }
    const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
    const currentDate = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
    const hh = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
    const mm = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
    const ss = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
    return `${date.getFullYear() + splitchar + month + splitchar + currentDate} ${hh}:${mm}:${ss}`;
  },

  /**
   *  获得当天对应的天数
   *  @ jsondate json日期
   *  @ splitchar 分隔符
   */
  GetDateDay(jsondate) {
    if (!jsondate) {
      return '';
    }
    const reg = /^\/Date\((\d+)\)\/$/g;
    let date = null;
    if (reg.test(jsondate)) {
      date = new Date(Number.parseInt(jsondate.replace(reg, '$1'), 10));
    } else if (/^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}.\d+Z$/gi.test(jsondate)) {
      jsondate = jsondate.replace(/(^.+)(T)(.+)(\..+Z)/gi, '$1 $3');
      date = new Date(jsondate);
    } else {
      date = new Date(jsondate);
    }
    return date.getDate();
  },

  /**
   * 获取请求参数
   * @ name 参数名
   * @ operation 操作 空时原样返回，lower转小写，upper转大写
   * 返回参数值，如果不存在返回null
   */
  GetQueryParamet(name, operation) {
    const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
    const r = window.location.search.substr(1).match(reg);
    if (r != null) {
      if (operation === 'lower') {
        return unescape(r[2]).toLowerCase();
      }
      if (operation === 'upper') {
        return unescape(r[2]).toUpperCase();
      }
      return unescape(r[2]);
    }
    return null; // toUpperCase
  },

  /**
   * 设置Cookie 【js版】
   * @ name 名称
   * @ value 内容
   * @ time 保存时间
   * @ path [可选] 路径
   * 删除时time传 -1
   */
  MsgSetCookie(name, value, time, path) {
    path = path || '';
    const d = new Date();
    d.setTime(d.getTime() + time * 24 * 60 * 60 * 1000);
    const expires = `expires=${d.toUTCString()}`;
    path = `path=${path}`;
    document.cookie = `${name}=${value}; ${expires}; ${path}`;
  },

  /**
   * 读取Cookie 【js版】
   * @ name 名称
   */
  MsgGetCookie(name) {
    name = `${name}=`;
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
      let c = ca[i];
      while (c.charAt(0) == ' ') c = c.substring(1);
      if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return '';
  },

  /**
   * 生成Guid 唯一主键
   * 把英文和-全部替换成纯数字
   */
  Guid() {
    const result = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
      let r = (Math.random() * 16) | 0,
        v = c == 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
    return result.replace(/-/gi, parseInt(Math.random() * 10)).replace(/[a-zA-Z]/gi, parseInt(Math.random() * 10));
  },

  /**
   * Base64加密解密类
   * @ .encode() 加密
   * @ .decode() 解密
   */
  Base64() {
    const _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    this.encode = function(input) {
      input = input || '';
      let output = '';
      let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
      let i = 0;
      input = _utf8_encode(input);
      while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
          enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
          enc4 = 64;
        }
        output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
      }
      return output;
    };
    this.decode = function(input) {
      input = input || '';
      let output = '';
      let chr1, chr2, chr3;
      let enc1, enc2, enc3, enc4;
      let i = 0;
      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
      while (i < input.length) {
        enc1 = _keyStr.indexOf(input.charAt(i++));
        enc2 = _keyStr.indexOf(input.charAt(i++));
        enc3 = _keyStr.indexOf(input.charAt(i++));
        enc4 = _keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output += String.fromCharCode(chr1);
        if (enc3 != 64) {
          output += String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
          output += String.fromCharCode(chr3);
        }
      }
      output = _utf8_decode(output);
      return output;
    };
    var _utf8_encode = function(string) {
      string = string.replace(/\r\n/g, '\n');
      let utftext = '';
      for (let n = 0; n < string.length; n++) {
        const c = string.charCodeAt(n);
        if (c < 128) {
          utftext += String.fromCharCode(c);
        } else if (c > 127 && c < 2048) {
          utftext += String.fromCharCode((c >> 6) | 192);
          utftext += String.fromCharCode((c & 63) | 128);
        } else {
          utftext += String.fromCharCode((c >> 12) | 224);
          utftext += String.fromCharCode(((c >> 6) & 63) | 128);
          utftext += String.fromCharCode((c & 63) | 128);
        }
      }
      return utftext;
    };
    var _utf8_decode = function(utftext) {
      let string = '';
      let i = 0;
      let c = (c1 = c2 = 0);
      while (i < utftext.length) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
          string += String.fromCharCode(c);
          i++;
        } else if (c > 191 && c < 224) {
          c2 = utftext.charCodeAt(i + 1);
          string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
          i += 2;
        } else {
          c2 = utftext.charCodeAt(i + 1);
          c3 = utftext.charCodeAt(i + 2);
          string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
          i += 3;
        }
      }
      return string;
    };
  },

  /**
   * 打印对象属性和方法
   * @ obj 对象
   */
  ShowObjectAttr(obj) {
    Object.keys(obj).forEach(name => {
      console.log(`${name}: ${obj[name]}`);
    });
  },

  /**
   * 获取纯文字
   * @ html 富文本/html标签
   * @ length 长度 可选[默认全部]
   */
  MsgGetPureText(html, length) {
    const text = html.replace(/<[^>]+>/g, '');
    length = Number.isNaN(length) ? text.length : length;
    return `${text.substring(0, length)}...`;
  },

  /**
   * 翻译状态
   * @ state
   */
  TranslateState(state) {
    switch (state) {
      case '0':
        return '待受理';
      case '1':
        return '待受理';
      case '2':
        return '已受理';
      case '3':
        return '不受理';
      case '4':
        return '已取消';
      case '5':
        return '被取消';
      default:
        return '未定义';
    }
  },

  /**
   * 邮件判断
   * @ email
   */
  ValidateEmail(email) {
    const myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return myreg.test(email);
  },

  /**
   * 电话判断
   * @ phone
   */
  ValidatePhone(phone) {
    const myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    return myreg.test(phone);
  },

  /**
   * 加载js文件
   * @ path js文件路径
   * @ callback 回调方法
   */
  MsgRequirejs(path, callback) {
    String.prototype.endWith = function(endStr) {
      const d = this.length - endStr.length;
      return d >= 0 && this.lastIndexOf(endStr) == d;
    };
    if (!path.endWith('.js')) {
      console.error('加载文件的类型不是js');
      return;
    }
    const elem = document.createElement('script');
    const scripts = document.getElementsByTagName('script');
    elem.setAttribute('type', 'text/javascript');
    elem.setAttribute('src', path);
    var total = scripts.length;
    for (let i = 0; i < total; i++) {
      if (scripts[i].getAttribute('src') === path) {
        var total = 0,
          timer = setInterval(() => {
            try {
              total++;
              callback();
              clearInterval(timer);
            } catch (err) {
              console.debug('正在加载文件，请稍候...');
              if (total > 20) {
                clearInterval(timer);
              }
            }
          }, 500);
        return;
      }
      if (i === total - 1) {
        document.body.appendChild(elem);
      }
    }
    if (typeof callback === 'function') {
      elem.onload = elem.onreadystatechange = function() {
        callback();
      };
    }
  },

  /**
   * 判断文件是否为图片类型
   * @ filename 文件名
   */
  validateImageExt(filename) {
    if (!filename) {
      return false;
    }
    const extStart = filename.lastIndexOf('.');
    const ext = filename.substring(extStart, filename.length).toUpperCase();
    const exts = ['.BMP', '.PNG', '.GIF', '.JPG', '.JPEG'];
    if (exts.indexOf(ext) > -1) {
      return true;
    }
    return false;
  },

  /**
   * 路由过滤，获取当前页面token
   * 一般是路由beforeEach钩子中调用
   * [router.options.routes循环调用]
   */
  filterRouter(router, path) {
    if (router.path === path) {
      return router.token;
    }
    if (!router.children) {
      return '';
    }
    for (let i = 0; i < router.children.length; i++) {
      const _token = this.filterRouter(router.children[i], path);
      if (_token) {
        return _token;
      }
    }
  },

  /**
   * 判断对象是否为空对象
   * @obj object
   */
  isEmptyObject(obj) {
    return typeof obj === 'object' && Object.keys(obj).length > 0;
  },

  /**
   * 系统token-session(外部不可见)
   */
  _SysTokenSession: {},

  /*
   * 设置token-session
   *   默认设置到JavascriptCommon中
   *   其次设置到localStorage中
   *   再次设置到cookie中
   */
  SetSysTokenSession(name, token) {
    this._SysTokenSession[name] = token;
    if (typeof window.localStorage === 'object') {
      localStorage.setItem(`SysTokenSession.${name}`, token);
    } else {
      this.MsgSetCookie(`SysTokenSession.${name}`, token, (1 / 24 / 60) * 30);
    }
  },

  /**
   * 读取token-session
   *   默认读取JavascriptCommon
   *   其次读取localStorage
   *   再次读取cookie
   */
  GetSysTokenSession(name) {
    if (!this._SysTokenSession[name]) {
      if (typeof window.localStorage === 'object') {
        return localStorage.getItem(`SysTokenSession.${name}`);
      }
      return this.MsgGetCookie(`SysTokenSession.${name}`);
    }
    return this._SysTokenSession[name];
  },

  /**
   * 清除token-session
   */
  RemoveSysTokenSession(name) {
    this._SysTokenSession[name] = undefined;
    if (typeof window.localStorage === 'object') {
      localStorage.removeItem(`SysTokenSession.${name}`);
    }
    this.MsgSetCookie(`SysTokenSession.${name}`, '', -1);
  },
};
